#include "UMemo.h"
#include "UControl.h"

TMemo::TMemo(TRectangle rect, TGLFontRef fnt, RGBAColor fgc, RGBAColor bgc)
: TControl(rect), fgcol(fgc), bgcol(bgc)
{
	reverseoutline = false;
	lastclickindex = false;
}

void TMemo::MouseDown	(int X, int Y, uint8 button)
{
	TControl::MouseDown(X, Y, button);
	lastclickindex = (Y-Top) / font->Height();
}

void TMemo::MouseUp	(int X, int Y, uint8 button)
{
	TControl::MouseUp(X, Y, button);
}

void TMemo::MouseClick()
{
	TControl::MouseClick();
	if (onClick)
		onClick(lastclickindex);
}

void TMemo::Draw( )
{
	glColor3ub(bgcol.c.r, bgcol.c.g, bgcol.c.b);
	glBegin(GL_QUADS);
		glVertex2i(Left		, Top);
		glVertex2i(Right	, Top);
		glVertex2i(Right	, Bottom);
		glVertex2i(Left		, Bottom);
	glEnd();

	int lines = (Bottom-Top-4) / font->Height();

	if (reverseoutline) {
		for (uint i = Lines.size(); lines > 0 && i-- != 0; --lines) {
			string s = Lines[i];
			glColor3ub(fgcol.c.r, fgcol.c.g, fgcol.c.b);
			font->drawText(float(Left+5), Top + 2 + (lines-1)*font->Height(), s);
		}
	} else {
		for (uint i = 0; i < Lines.size() && i < lines; ++i) {
			string s = Lines[i];
			glColor3ub(fgcol.c.r, fgcol.c.g, fgcol.c.b);
			font->drawText(float(Left+5), Top + 2 + i*font->Height(), s);
		}
	}
}
